
#include <SDL.h>
#include "ScoreBoardManager.h"
#include "Game.h"

ScoreBoardManager* ScoreBoardManager::singleton = NULL; 

ScoreBoardManager::ScoreBoardManager() {
	this->buildStaticShapes();
	this->buildDynamicShapes();
}

ScoreBoardManager* ScoreBoardManager::getInstance() {
	if(!singleton) {
		singleton = new ScoreBoardManager();
	}
	return singleton;
}

ScoreBoardManager::~ScoreBoardManager() {
;
}

vector<Shape*> ScoreBoardManager::getDynamicShapes(){
	return dynamicShapes;
}

void ScoreBoardManager::setDynamicShapes(vector<Shape*> elems){
	this->dynamicShapes = elems;
}

vector<Shape*> ScoreBoardManager::getStaticShapes(){
	return staticShapes;
}

void ScoreBoardManager::setStaticShapes(vector<Shape*> elems){
	this->staticShapes = elems;
}


//este metodo va a construir las imagenes que son fijas en el ScoreBoard y
//las va a poner todas en el vector staticShapes
void ScoreBoardManager::buildStaticShapes(){
	Game* game = Game::getInstance();
	
	//Construyendo el rectangulo de las vidas
	Rectangle* livesTitle = new Rectangle();
	livesTitle->setId("livesRect");
	livesTitle->setBase(80);
	livesTitle->setAltura(40);
	Point* livesPositionPoint = new Point();
	livesPositionPoint->setX(game->getBoardInitialX()+20);
	livesPositionPoint->setY(game->getBoardInitialY()+20);
	livesTitle->setPosicion(livesPositionPoint);
	livesTitle->setIdTextura("vidas.bmp");
	livesTitle->setLineThickness(0);
	livesTitle->setInterpolate(false);
	livesTitle->setType(WALL);
	this->staticShapes.push_back(livesTitle);

	//Construyendo el rectangulo del Score
	Rectangle* scoreTitle = new Rectangle();
	scoreTitle->setId("scoreRect");
	scoreTitle->setBase(85);
	scoreTitle->setAltura(40);
	Point* scorePositionPoint = new Point();
	scorePositionPoint->setX(livesPositionPoint->getX());
	scorePositionPoint->setY(livesPositionPoint->getY()+100);
	scoreTitle->setPosicion(scorePositionPoint);
	scoreTitle->setIdTextura("puntos.bmp");
	scoreTitle->setLineThickness(0);
	scoreTitle->setInterpolate(false);
	scoreTitle->setType(WALL);
	this->staticShapes.push_back(scoreTitle);

	//Construyendo el rectangulo del Nivel
	Rectangle* levelTitle = new Rectangle();
	levelTitle->setId("levelRect");
	levelTitle->setBase(80);
	levelTitle->setAltura(40);
	Point* levelPositionPoint = new Point();
	levelPositionPoint->setX(scorePositionPoint->getX());
	levelPositionPoint->setY(scorePositionPoint->getY()+100);
	levelTitle->setPosicion(levelPositionPoint);
	levelTitle->setIdTextura("nivel.bmp");
	levelTitle->setLineThickness(0);
	levelTitle->setInterpolate(false);
	levelTitle->setType(WALL);
	this->staticShapes.push_back(levelTitle);

}

//Este metod va a construir las imagenes que son dinamicas en el ScoreBoard y
//las va a agregar al vector dynamicShapes
void ScoreBoardManager::buildDynamicShapes(){
	
	Game* game = Game::getInstance();

	//Construyendo el rectangulo de las vidas 
	Rectangle* lives= new Rectangle();
	lives->setId("lives");
	lives->setBase(20);
	lives->setAltura(30);
	Point* livesPositionPoint = new Point();
	livesPositionPoint->setX(game->getBoardInitialX()+100);
	livesPositionPoint->setY(game->getBoardInitialY()+60);
	lives->setPosicion(livesPositionPoint);
	lives->setIdTextura("cero.bmp");
	lives->setLineThickness(0);
	lives->setInterpolate(false);
	lives->setType(WALL);
	this->dynamicShapes.push_back(lives);


	//Construyendo los rectangulos de los puntos
	Rectangle* score10000= new Rectangle();
	score10000->setId("score10000");
	score10000->setBase(20);
	score10000->setAltura(30);
	Point* score10000PositionPoint = new Point();
	score10000PositionPoint->setX(livesPositionPoint->getX()-40);
	score10000PositionPoint->setY(livesPositionPoint->getY()+100);
	score10000->setPosicion(score10000PositionPoint);
	score10000->setIdTextura("cero.bmp");
	score10000->setLineThickness(0);
	score10000->setInterpolate(false);
	score10000->setType(WALL);
	this->dynamicShapes.push_back(score10000);

	Rectangle* score01000= new Rectangle();
	score01000->setId("score01000");
	score01000->setBase(20);
	score01000->setAltura(30);
	Point* score01000PositionPoint = new Point();
	score01000PositionPoint->setX(score10000PositionPoint->getX()+25);
	score01000PositionPoint->setY(score10000PositionPoint->getY());
	score01000->setPosicion(score01000PositionPoint);
	score01000->setIdTextura("cero.bmp");
	score01000->setLineThickness(0);
	score01000->setInterpolate(false);
	score01000->setType(WALL);
	this->dynamicShapes.push_back(score01000);

	Rectangle* score00100= new Rectangle();
	score00100->setId("score00100");
	score00100->setBase(20);
	score00100->setAltura(30);
	Point* score00100PositionPoint = new Point();
	score00100PositionPoint->setX(score01000PositionPoint->getX()+25);
	score00100PositionPoint->setY(score01000PositionPoint->getY());
	score00100->setPosicion(score00100PositionPoint);
	score00100->setIdTextura("cero.bmp");
	score00100->setLineThickness(0);
	score00100->setInterpolate(false);
	score00100->setType(WALL);
	this->dynamicShapes.push_back(score00100);

	Rectangle* score00010= new Rectangle();
	score00010->setId("score00010");
	score00010->setBase(20);
	score00010->setAltura(30);
	Point* score00010PositionPoint = new Point();
	score00010PositionPoint->setX(score00100PositionPoint->getX()+25);
	score00010PositionPoint->setY(score00100PositionPoint->getY());
	score00010->setPosicion(score00010PositionPoint);
	score00010->setIdTextura("cero.bmp");
	score00010->setLineThickness(0);
	score00010->setInterpolate(false);
	score00010->setType(WALL);
	this->dynamicShapes.push_back(score00010);

	Rectangle* score00001= new Rectangle();
	score00001->setId("score00001");
	score00001->setBase(20);
	score00001->setAltura(30);
	Point* score00001PositionPoint = new Point();
	score00001PositionPoint->setX(score00010PositionPoint->getX()+25);
	score00001PositionPoint->setY(score00010PositionPoint->getY());
	score00001->setPosicion(score00001PositionPoint);
	score00001->setIdTextura("cero.bmp");
	score00001->setLineThickness(0);
	score00001->setInterpolate(false);
	score00001->setType(WALL);
	this->dynamicShapes.push_back(score00001);

	//Construyendo el rectangulo del nivel
	Rectangle* level= new Rectangle();
	level->setId("lives");
	level->setBase(20);
	level->setAltura(30);
	Point* levelPositionPoint = new Point();
	levelPositionPoint->setX(livesPositionPoint->getX());
	levelPositionPoint->setY(livesPositionPoint->getY()+200);
	level->setPosicion(levelPositionPoint);
	level->setIdTextura("cero.bmp");
	level->setLineThickness(0);
	level->setInterpolate(false);
	level->setType(WALL);
	this->dynamicShapes.push_back(level);
}


//este metodo va a updatear las figuras dinamicas para que tengan la textura correcta.
void ScoreBoardManager::updateDynamicShapes(int level, int score, int lives){

	string scStr = intToString(score);
	string scoreStr;
	for (int i=0; i<scStr.size(); i++){
		if ((scStr.at((scStr.size()-i)-1)) == '0'){
			scoreStr = "cero.bmp";
		} else if((scStr.at((scStr.size()-i)-1)) == '1') {
			scoreStr = "uno.bmp";
		} else if ((scStr.at((scStr.size()-i)-1)) == '2') {
			scoreStr = "dos.bmp";
		} else if ((scStr.at((scStr.size()-i)-1)) == '3') {
			scoreStr = "tres.bmp";
		} else if ((scStr.at((scStr.size()-i)-1)) == '4') {
			scoreStr = "cuatro.bmp";
		} else if ((scStr.at((scStr.size()-i)-1)) == '5') {
			scoreStr = "cinco.bmp";
		} else if ((scStr.at((scStr.size()-i)-1)) == '6') {
			scoreStr = "seis.bmp";
		} else if ((scStr.at((scStr.size()-i)-1)) == '7') {
			scoreStr = "siete.bmp";
		} else if ((scStr.at((scStr.size()-i)-1)) == '8') {
			scoreStr = "ocho.bmp";
		} else if ((scStr.at((scStr.size()-i)-1)) == '9') {
			scoreStr = "nueve.bmp";
		} 

		char* scoreChar = new char[scoreStr.size()+1];
		strcpy(scoreChar,scoreStr.c_str());
		dynamicShapes[(5-i)]->setIdTextura(scoreChar);
	}

	
	//Genero las vidas nuevas
	string livesString;
	switch (lives){
	  case 0:
		livesString = "cero.bmp";
		break;
	  case 1:
		livesString = "uno.bmp";
		break;
	  case 2:
		livesString = "dos.bmp";
		break;
	  case 3:
		livesString = "tres.bmp";
		break;
	  case 4:
		livesString = "cuatro.bmp";
		break;
	  case 5:
		livesString = "cinco.bmp";
		break;
	  case 6:
		livesString = "seis.bmp";
		break;
	  case 7:
		livesString = "siete.bmp";
		break;
	  case 8:
		livesString = "ocho.bmp";
		break;
	  case 9:
		livesString = "nueve.bmp";
		break;
	  default:
		printf("Esta todo mal si entro aca.\n");
	}

	char* livesChar = new char[livesString.size()+1];
	strcpy(livesChar,livesString.c_str());
	dynamicShapes[0]->setIdTextura(livesChar);


	//Level
	//Genero las vidas nuevas
	string levelString;

	switch (level){
	  case 0:
		levelString = "cero.bmp";
		break;
	  case 1:
		levelString = "uno.bmp";
		break;
	  case 2:
		levelString = "dos.bmp";
		break;
	  case 3:
		levelString = "tres.bmp";
		break;
	  case 4:
		levelString = "cuatro.bmp";
		break;
	  case 5:
		levelString = "cinco.bmp";
		break;
	  case 6:
		levelString = "seis.bmp";
		break;
	  case 7:
		levelString = "siete.bmp";
		break;
	  case 8:
		levelString = "ocho.bmp";
		break;
	  case 9:
		levelString = "nueve.bmp";
		break;
	  default:
		printf("Esta todo mal si entro aca.\n");
	}

	char* levelChar = new char[levelString.size()+1];
	strcpy(levelChar,levelString.c_str());
	dynamicShapes[6]->setIdTextura(levelChar);

}

//este metodo va a dibujar todo el ScoreBoard.
void ScoreBoardManager::draw(int level, int score, int lives, SDL_Surface* screen){
	
	for (int i=0; i<staticShapes.size(); i++){
		staticShapes[i]->draw(screen);
	}


	updateDynamicShapes(level, score, lives);


	for (int j=0; j<dynamicShapes.size(); j++){
		dynamicShapes[j]->draw(screen);
	}
}